package com.cp.admin.modular.system.controller;

import com.cp.admin.api.utils.PKGeneratorUtils;
import com.cp.admin.core.shiro.ShiroKit;
import com.cp.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cp.admin.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cp.admin.modular.system.model.Message;
import com.cp.admin.modular.system.service.IMessageService;

import java.util.Date;

/**
 * 公告消息控制器
 *
 * @author fengshuonan
 * @Date 2020-04-30 16:27:51
 */
@Controller
@RequestMapping("/message")
public class MessageController extends BaseController {

    private String PREFIX = "/system/message/";

    @Autowired
    private IMessageService messageService;

    /**
     * 跳转到公告消息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "message.html";
    }

    /**
     * 跳转到添加公告消息
     */
    @RequestMapping("/message_add")
    public String messageAdd() {
        return PREFIX + "message_add.html";
    }

    /**
     * 跳转到修改公告消息
     */
    @RequestMapping("/message_update/{messageId}")
    public String messageUpdate(@PathVariable Long messageId, Model model) {
        Message message = messageService.selectById(messageId);
        model.addAttribute("item",message);
        LogObjectHolder.me().set(message);
        return PREFIX + "message_edit.html";
    }

    /**
     * 获取公告消息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return messageService.selectList(null);
    }

    /**
     * 新增公告消息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Message message) {
        Date date = new Date();
        message.setId(PKGeneratorUtils.getInstance().nextId());
        message.setType(0);
        message.setSysUserId(Long.parseLong(ShiroKit.getUser().getId()+""));
        message.setSysUserName(ShiroKit.getUser().getName());
        message.setStatus(0);
        message.setCreateTime(date);
        message.setUpdateTime(date);
        messageService.insert(message);
        return SUCCESS_TIP;
    }

    /**
     * 删除公告消息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long messageId) {
        messageService.deleteById(messageId);
        return SUCCESS_TIP;
    }

    /**
     * 修改公告消息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Message message) {
        messageService.updateById(message);
        return SUCCESS_TIP;
    }

    /**
     * 公告消息详情
     */
    @RequestMapping(value = "/detail/{messageId}")
    @ResponseBody
    public Object detail(@PathVariable("messageId") Long messageId) {
        return messageService.selectById(messageId);
    }
}
